
<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>pymatgen.command_line.mcsqs_caller &#8212; pymatgen 2020.7.3 documentation</title>
    <link rel="stylesheet" href="../../../_static/basic.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script src="../../../_static/jquery.js"></script>
    <script src="../../../_static/underscore.js"></script>
    <script src="../../../_static/doctools.js"></script>
    <script src="../../../_static/language_data.js"></script>
    <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
 
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-33990148-1']);
  _gaq.push(['_trackPageview']);
</script>

  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../pymatgen.html" accesskey="U">pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.command_line.mcsqs_caller</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
          <div class="body" role="main">
            
  <h1>Source code for pymatgen.command_line.mcsqs_caller</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Module to call mcsqs, distributed with AT-AT</span>
<span class="sd">https://www.brown.edu/Departments/Engineering/Labs/avdw/atat/</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">warnings</span>
<span class="kn">from</span> <span class="nn">subprocess</span> <span class="kn">import</span> <span class="n">Popen</span><span class="p">,</span> <span class="n">TimeoutExpired</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Dict</span><span class="p">,</span> <span class="n">Union</span><span class="p">,</span> <span class="n">List</span><span class="p">,</span> <span class="n">NamedTuple</span><span class="p">,</span> <span class="n">Optional</span>
<span class="kn">from</span> <span class="nn">pathlib</span> <span class="kn">import</span> <span class="n">Path</span>

<span class="kn">from</span> <span class="nn">monty.dev</span> <span class="kn">import</span> <span class="n">requires</span>
<span class="kn">from</span> <span class="nn">monty.os.path</span> <span class="kn">import</span> <span class="n">which</span>
<span class="kn">import</span> <span class="nn">tempfile</span>

<span class="kn">from</span> <span class="nn">pymatgen</span> <span class="kn">import</span> <span class="n">Structure</span>


<div class="viewcode-block" id="Sqs"><a class="viewcode-back" href="../../../pymatgen.command_line.mcsqs_caller.html#pymatgen.command_line.mcsqs_caller.Sqs">[docs]</a><span class="k">class</span> <span class="nc">Sqs</span><span class="p">(</span><span class="n">NamedTuple</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Return type for run_mcsqs.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">bestsqs</span><span class="p">:</span> <span class="n">Structure</span>
    <span class="n">objective_function</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">float</span><span class="p">,</span> <span class="nb">str</span><span class="p">]</span>
    <span class="n">allsqs</span><span class="p">:</span> <span class="n">List</span>
    <span class="n">directory</span><span class="p">:</span> <span class="nb">str</span></div>


<div class="viewcode-block" id="run_mcsqs"><a class="viewcode-back" href="../../../pymatgen.command_line.mcsqs_caller.html#pymatgen.command_line.mcsqs_caller.run_mcsqs">[docs]</a><span class="nd">@requires</span><span class="p">(</span>
    <span class="n">which</span><span class="p">(</span><span class="s2">&quot;mcsqs&quot;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">which</span><span class="p">(</span><span class="s2">&quot;str2cif&quot;</span><span class="p">),</span>
    <span class="s2">&quot;run_mcsqs requires first installing AT-AT, &quot;</span>
    <span class="s2">&quot;see https://www.brown.edu/Departments/Engineering/Labs/avdw/atat/&quot;</span><span class="p">,</span>
<span class="p">)</span>
<span class="k">def</span> <span class="nf">run_mcsqs</span><span class="p">(</span>
    <span class="n">structure</span><span class="p">:</span> <span class="n">Structure</span><span class="p">,</span>
    <span class="n">clusters</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="nb">float</span><span class="p">],</span>
    <span class="n">scaling</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="n">List</span><span class="p">[</span><span class="nb">int</span><span class="p">]],</span>
    <span class="n">search_time</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mi">60</span><span class="p">,</span>
    <span class="n">directory</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
    <span class="n">instances</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
    <span class="n">temperature</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="nb">float</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span>
    <span class="n">wr</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span>
    <span class="n">wn</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span>
    <span class="n">wd</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">0.5</span><span class="p">,</span>
    <span class="n">tol</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">1e-3</span><span class="p">,</span>
<span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Sqs</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Helper function for calling mcsqs with different arguments</span>
<span class="sd">    Args:</span>
<span class="sd">        structure (Structure): Disordered pymatgen Structure object</span>
<span class="sd">        clusters (dict): Dictionary of cluster interactions with entries in the form</span>
<span class="sd">            number of atoms: cutoff in angstroms</span>
<span class="sd">        scaling (int or list): Scaling factor to determine supercell. Two options are possible:</span>
<span class="sd">                a. (preferred) Scales number of atoms, e.g., for a structure with 8 atoms,</span>
<span class="sd">                   scaling=4 would lead to a 32 atom supercell</span>
<span class="sd">                b. A sequence of three scaling factors, e.g., [2, 1, 1], which</span>
<span class="sd">                   specifies that the supercell should have dimensions 2a x b x c</span>
<span class="sd">    Keyword Args:</span>
<span class="sd">        search_time (float): Time spent looking for the ideal SQS in minutes (default: 60)</span>
<span class="sd">        directory (str): Directory to run mcsqs calculation and store files (default: None</span>
<span class="sd">            runs calculations in a temp directory)</span>
<span class="sd">        instances (int): Specifies the number of parallel instances of mcsqs to run</span>
<span class="sd">            (default: number of cpu cores detected by Python)</span>
<span class="sd">        temperature (int or float): Monte Carlo temperature (default: 1), &quot;T&quot; in atat code</span>
<span class="sd">        wr (int or float): Weight assigned to range of perfect correlation match in objective</span>
<span class="sd">            function (default = 1)</span>
<span class="sd">        wn (int or float): Multiplicative decrease in weight per additional point in cluster (default: 1)</span>
<span class="sd">        wd (int or float): Exponent of decay in weight as function of cluster diameter (default: 0.5)</span>
<span class="sd">        tol (int or float): Tolerance for matching correlations (default: 1e-3)</span>

<span class="sd">    Returns:</span>
<span class="sd">        Tuple of Pymatgen structure SQS of the input structure, the mcsqs objective function,</span>
<span class="sd">            list of all SQS structures, and the directory where calculations are run</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">num_atoms</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">structure</span><span class="o">.</span><span class="n">is_ordered</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Pick a disordered structure&quot;</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">instances</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="c1"># os.cpu_count() can return None if detection fails</span>
        <span class="n">instances</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">cpu_count</span><span class="p">()</span>

    <span class="n">original_directory</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">getcwd</span><span class="p">()</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">directory</span><span class="p">:</span>
        <span class="n">directory</span> <span class="o">=</span> <span class="n">tempfile</span><span class="o">.</span><span class="n">mkdtemp</span><span class="p">()</span>
    <span class="n">os</span><span class="o">.</span><span class="n">chdir</span><span class="p">(</span><span class="n">directory</span><span class="p">)</span>

    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">scaling</span><span class="p">,</span> <span class="p">(</span><span class="nb">int</span><span class="p">,</span> <span class="nb">float</span><span class="p">)):</span>

        <span class="k">if</span> <span class="n">scaling</span> <span class="o">%</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Scaling should be an integer, not </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">scaling</span><span class="p">))</span>
        <span class="n">mcsqs_find_sqs_cmd</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;mcsqs&quot;</span><span class="p">,</span> <span class="s2">&quot;-n </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">scaling</span> <span class="o">*</span> <span class="n">num_atoms</span><span class="p">)]</span>

    <span class="k">else</span><span class="p">:</span>

        <span class="c1"># Set supercell to identity (will make supercell with pymatgen)</span>
        <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;sqscell.out&quot;</span><span class="p">,</span> <span class="s2">&quot;w&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
            <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s2">&quot;1</span><span class="se">\n</span><span class="s2">1 0 0</span><span class="se">\n</span><span class="s2">0 1 0</span><span class="se">\n</span><span class="s2">0 0 1</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
        <span class="n">structure</span> <span class="o">=</span> <span class="n">structure</span> <span class="o">*</span> <span class="n">scaling</span>
        <span class="n">mcsqs_find_sqs_cmd</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;mcsqs&quot;</span><span class="p">,</span> <span class="s2">&quot;-rc&quot;</span><span class="p">,</span> <span class="s2">&quot;-n </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">num_atoms</span><span class="p">)]</span>

    <span class="n">structure</span><span class="o">.</span><span class="n">to</span><span class="p">(</span><span class="n">filename</span><span class="o">=</span><span class="s2">&quot;rndstr.in&quot;</span><span class="p">)</span>

    <span class="c1"># Generate clusters</span>
    <span class="n">mcsqs_generate_clusters_cmd</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;mcsqs&quot;</span><span class="p">]</span>
    <span class="k">for</span> <span class="n">num</span> <span class="ow">in</span> <span class="n">clusters</span><span class="p">:</span>
        <span class="n">mcsqs_generate_clusters_cmd</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;-&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">num</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;=&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">clusters</span><span class="p">[</span><span class="n">num</span><span class="p">]))</span>

    <span class="c1"># Run mcsqs to find clusters</span>
    <span class="n">p</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">(</span><span class="n">mcsqs_generate_clusters_cmd</span><span class="p">)</span>
    <span class="n">p</span><span class="o">.</span><span class="n">communicate</span><span class="p">()</span>

    <span class="c1"># Generate SQS structures</span>
    <span class="n">add_ons</span> <span class="o">=</span> <span class="p">[</span>
        <span class="s2">&quot;-T </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">temperature</span><span class="p">),</span>
        <span class="s2">&quot;-wr </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">wr</span><span class="p">),</span>
        <span class="s2">&quot;-wn </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">wn</span><span class="p">),</span>
        <span class="s2">&quot;-wd </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">wd</span><span class="p">),</span>
        <span class="s2">&quot;-tol </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">tol</span><span class="p">),</span>
    <span class="p">]</span>

    <span class="n">mcsqs_find_sqs_processes</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">if</span> <span class="n">instances</span> <span class="ow">and</span> <span class="n">instances</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
        <span class="c1"># if multiple instances, run a range of commands using &quot;-ip&quot;</span>
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">instances</span><span class="p">):</span>
            <span class="n">instance_cmd</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&quot;-ip </span><span class="si">{}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)]</span>
            <span class="n">cmd</span> <span class="o">=</span> <span class="n">mcsqs_find_sqs_cmd</span> <span class="o">+</span> <span class="n">add_ons</span> <span class="o">+</span> <span class="n">instance_cmd</span>
            <span class="n">p</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">(</span><span class="n">cmd</span><span class="p">)</span>
            <span class="n">mcsqs_find_sqs_processes</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="c1"># run normal mcsqs command</span>
        <span class="n">cmd</span> <span class="o">=</span> <span class="n">mcsqs_find_sqs_cmd</span> <span class="o">+</span> <span class="n">add_ons</span>
        <span class="n">p</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">(</span><span class="n">cmd</span><span class="p">)</span>
        <span class="n">mcsqs_find_sqs_processes</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>

    <span class="k">try</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">p</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">mcsqs_find_sqs_processes</span><span class="p">):</span>
            <span class="n">p</span><span class="o">.</span><span class="n">communicate</span><span class="p">(</span><span class="n">timeout</span><span class="o">=</span><span class="n">search_time</span> <span class="o">*</span> <span class="mi">60</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">instances</span> <span class="ow">and</span> <span class="n">instances</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">p</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">([</span><span class="s2">&quot;mcsqs&quot;</span><span class="p">,</span> <span class="s2">&quot;-best&quot;</span><span class="p">])</span>
            <span class="n">p</span><span class="o">.</span><span class="n">communicate</span><span class="p">()</span>

        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s2">&quot;bestsqs.out&quot;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s2">&quot;bestcorr.out&quot;</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">_parse_sqs_path</span><span class="p">(</span><span class="s2">&quot;.&quot;</span><span class="p">)</span>

        <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">&quot;mcsqs exited before timeout reached&quot;</span><span class="p">)</span>

    <span class="k">except</span> <span class="n">TimeoutExpired</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">mcsqs_find_sqs_processes</span><span class="p">:</span>
            <span class="n">p</span><span class="o">.</span><span class="n">kill</span><span class="p">()</span>
            <span class="n">p</span><span class="o">.</span><span class="n">communicate</span><span class="p">()</span>

        <span class="c1"># Find the best sqs structures</span>
        <span class="k">if</span> <span class="n">instances</span> <span class="ow">and</span> <span class="n">instances</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>

            <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s2">&quot;bestcorr1.out&quot;</span><span class="p">):</span>
                <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span>
                    <span class="s2">&quot;mcsqs did not generate output files, &quot;</span>
                    <span class="s2">&quot;is search_time sufficient or are number of instances too high?&quot;</span>
                <span class="p">)</span>

            <span class="n">p</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">([</span><span class="s2">&quot;mcsqs&quot;</span><span class="p">,</span> <span class="s2">&quot;-best&quot;</span><span class="p">])</span>
            <span class="n">p</span><span class="o">.</span><span class="n">communicate</span><span class="p">()</span>

        <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s2">&quot;bestsqs.out&quot;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="s2">&quot;bestcorr.out&quot;</span><span class="p">):</span>
            <span class="n">sqs</span> <span class="o">=</span> <span class="n">_parse_sqs_path</span><span class="p">(</span><span class="s2">&quot;.&quot;</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">sqs</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="n">os</span><span class="o">.</span><span class="n">chdir</span><span class="p">(</span><span class="n">original_directory</span><span class="p">)</span>
            <span class="k">raise</span> <span class="ne">TimeoutError</span><span class="p">(</span><span class="s2">&quot;Cluster expansion took too long.&quot;</span><span class="p">)</span></div>


<span class="k">def</span> <span class="nf">_parse_sqs_path</span><span class="p">(</span><span class="n">path</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Sqs</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Private function to parse mcsqs output directory</span>
<span class="sd">    Args:</span>
<span class="sd">        path: directory to perform parsing</span>

<span class="sd">    Returns:</span>
<span class="sd">        Tuple of Pymatgen structure SQS of the input structure, the mcsqs objective function,</span>
<span class="sd">            list of all SQS structures, and the directory where calculations are run</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">path</span> <span class="o">=</span> <span class="n">Path</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>

    <span class="c1"># detected instances will be 0 if mcsqs was run in series, or number of instances</span>
    <span class="n">detected_instances</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">path</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="s2">&quot;bestsqs*[0-9]*&quot;</span><span class="p">)))</span>

    <span class="c1"># Convert best SQS structure to cif file and pymatgen Structure</span>
    <span class="n">p</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">(</span><span class="s2">&quot;str2cif &lt; bestsqs.out &gt; bestsqs.cif&quot;</span><span class="p">,</span> <span class="n">shell</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">cwd</span><span class="o">=</span><span class="n">path</span><span class="p">)</span>
    <span class="n">p</span><span class="o">.</span><span class="n">communicate</span><span class="p">()</span>

    <span class="k">with</span> <span class="n">warnings</span><span class="o">.</span><span class="n">catch_warnings</span><span class="p">():</span>
        <span class="n">warnings</span><span class="o">.</span><span class="n">simplefilter</span><span class="p">(</span><span class="s2">&quot;ignore&quot;</span><span class="p">)</span>
        <span class="n">bestsqs</span> <span class="o">=</span> <span class="n">Structure</span><span class="o">.</span><span class="n">from_file</span><span class="p">(</span><span class="n">path</span> <span class="o">/</span> <span class="s2">&quot;bestsqs.cif&quot;</span><span class="p">)</span>

    <span class="c1"># Get best SQS objective function</span>
    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">path</span> <span class="o">/</span> <span class="s2">&quot;bestcorr.out&quot;</span><span class="p">,</span> <span class="s2">&quot;r&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
        <span class="n">lines</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span>

    <span class="n">objective_function_str</span> <span class="o">=</span> <span class="n">lines</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;=&quot;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
    <span class="n">objective_function</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">float</span><span class="p">,</span> <span class="nb">str</span><span class="p">]</span>
    <span class="k">if</span> <span class="n">objective_function_str</span> <span class="o">!=</span> <span class="s2">&quot;Perfect_match&quot;</span><span class="p">:</span>
        <span class="n">objective_function</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">objective_function_str</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">objective_function</span> <span class="o">=</span> <span class="s2">&quot;Perfect_match&quot;</span>

    <span class="c1"># Get all SQS structures and objective functions</span>
    <span class="n">allsqs</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">detected_instances</span><span class="p">):</span>
        <span class="n">sqs_out</span> <span class="o">=</span> <span class="s2">&quot;bestsqs</span><span class="si">{}</span><span class="s2">.out&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
        <span class="n">sqs_cif</span> <span class="o">=</span> <span class="s2">&quot;bestsqs</span><span class="si">{}</span><span class="s2">.cif&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
        <span class="n">corr_out</span> <span class="o">=</span> <span class="s2">&quot;bestcorr</span><span class="si">{}</span><span class="s2">.out&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
        <span class="n">p</span> <span class="o">=</span> <span class="n">Popen</span><span class="p">(</span><span class="s2">&quot;str2cif &lt;&quot;</span> <span class="o">+</span> <span class="n">sqs_out</span> <span class="o">+</span> <span class="s2">&quot;&gt;&quot;</span> <span class="o">+</span> <span class="n">sqs_cif</span><span class="p">,</span> <span class="n">shell</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">cwd</span><span class="o">=</span><span class="n">path</span><span class="p">)</span>
        <span class="n">p</span><span class="o">.</span><span class="n">communicate</span><span class="p">()</span>
        <span class="k">with</span> <span class="n">warnings</span><span class="o">.</span><span class="n">catch_warnings</span><span class="p">():</span>
            <span class="n">warnings</span><span class="o">.</span><span class="n">simplefilter</span><span class="p">(</span><span class="s2">&quot;ignore&quot;</span><span class="p">)</span>
            <span class="n">sqs</span> <span class="o">=</span> <span class="n">Structure</span><span class="o">.</span><span class="n">from_file</span><span class="p">(</span><span class="n">sqs_cif</span><span class="p">)</span>
        <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">path</span> <span class="o">/</span> <span class="n">corr_out</span><span class="p">,</span> <span class="s2">&quot;r&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
            <span class="n">lines</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span>

        <span class="n">objective_function_str</span> <span class="o">=</span> <span class="n">lines</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;=&quot;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
        <span class="n">obj</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">float</span><span class="p">,</span> <span class="nb">str</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">objective_function_str</span> <span class="o">!=</span> <span class="s2">&quot;Perfect_match&quot;</span><span class="p">:</span>
            <span class="n">obj</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">objective_function_str</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">obj</span> <span class="o">=</span> <span class="s2">&quot;Perfect_match&quot;</span>
        <span class="n">allsqs</span><span class="o">.</span><span class="n">append</span><span class="p">({</span><span class="s2">&quot;structure&quot;</span><span class="p">:</span> <span class="n">sqs</span><span class="p">,</span> <span class="s2">&quot;objective_function&quot;</span><span class="p">:</span> <span class="n">obj</span><span class="p">})</span>

    <span class="k">return</span> <span class="n">Sqs</span><span class="p">(</span>
        <span class="n">bestsqs</span><span class="o">=</span><span class="n">bestsqs</span><span class="p">,</span>
        <span class="n">objective_function</span><span class="o">=</span><span class="n">objective_function</span><span class="p">,</span>
        <span class="n">allsqs</span><span class="o">=</span><span class="n">allsqs</span><span class="p">,</span>
        <span class="n">directory</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">path</span><span class="o">.</span><span class="n">resolve</span><span class="p">()),</span>
    <span class="p">)</span>
</pre></div>

            <div class="clearer"></div>
          </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pymatgen 2020.7.3 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="../../pymatgen.html" >pymatgen</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="">pymatgen.command_line.mcsqs_caller</a></li> 
      </ul>
    </div>

    <div class="footer" role="contentinfo">
        &#169; Copyright 2011, Pymatgen Development Team.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 3.1.2.
    </div>
<div class="footer">This page uses <a href="http://analytics.google.com/">
Google Analytics</a> to collect statistics. You can disable it by blocking
the JavaScript coming from www.google-analytics.com.
<script type="text/javascript">
  (function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ?
              'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
  })();
</script>
</div>

  </body>
</html>